package com.shundr.auth.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.shundr.auth.channel.R;
import com.shundr.auth.base.BaseActivity;
import com.shundr.auth.common.view.DialogAuth;
import com.shundr.auth.frame.constant.LContext;
import com.shundr.auth.frame.http.LHttpRequest;
import com.shundr.auth.frame.util.StringUtil;
import com.shundr.auth.frame.util.ToastHelper;
import com.shundr.auth.user.http.UserRequest;
import com.shundr.auth.user.model.UserInfo;

public class RegisterActivity extends BaseActivity {
	private EditText etName;
	private EditText etPw;
	private EditText etPwConfirm;
	private EditText etCode;

	private Button btnGetCode;
	private Button btnRegister;

	private RadioGroup rgType;

	private Timer mTimer;
	private static final int TOTAL = 120;
	private int timerTotal = TOTAL;

	private String userType = "0";// 0代表司机，1代表货主

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UserRequest.LOGIN_SUCCESSED:
				final UserInfo user = (UserInfo) msg.obj;
				if (user != null) {
					LContext.setACCESS_TOKEN(user.getAccessToken());
					LContext.userInfo = user;
					ToastHelper.toastShort(mContext, "注册成功！");
					DialogAuth dialogAuth = new DialogAuth(mContext, user);
					dialogAuth.show();
				}
				break;
			case UserRequest.CHECK_PHONE_SUCCESSED:
				new UserRequest(mContext, mHandler).getAuthCode(etName
						.getText().toString(), UserInfo.CatpchaType.REGISTER);
				break;
			case UserRequest.GET_AUTH_CODE_SUCCESSED:
				mTimer = new Timer();
				mTimer.schedule(new TimerTask() {

					@Override
					public void run() {
						mHandler.sendEmptyMessage(2000);
					}
				}, 0, 1000);
				break;
			case UserRequest.CHECK_CAPTCHA_SUCCESSED:
				// 注册
				new UserRequest(mContext, mHandler).register(userType, etName
						.getText().toString(), etPw.getText().toString(),
						etCode.getText().toString());
				break;
			case UserRequest.REGISTER_SUCCESSED:
				// 注册成功
				new UserRequest(mContext, mHandler).login(etName.getText()
						.toString(), etPw.getText().toString());
				break;
			case 2000:
				if (timerTotal != 0) {
					timerTotal = timerTotal - 1;
					btnGetCode.setText(timerTotal + "秒后重新获取");
					btnGetCode
							.setBackgroundResource(R.drawable.btn_gray_light_selector);
					btnGetCode.setEnabled(false);
				} else {
					timerTotal = TOTAL;
					btnGetCode.setText("点击重新获取");
					btnGetCode
							.setBackgroundResource(R.drawable.btn_orange_light_selector);
					btnGetCode.setEnabled(true);
					mTimer.cancel();
					mTimer = null;
				}
				break;
			case LHttpRequest.REQUEST_FAILED:
				ToastHelper.toastShort(mContext, msg.obj.toString());
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		btnRight.setVisibility(View.VISIBLE);
		btnRight.setText("直接登录认证");
		btnRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(mContext, LoginActivity.class));
			}
		});
		setContentView(R.layout.activity_register);
		initView();
	}

	private void initView() {
		etName = (EditText) findViewById(R.id.et_username);
		etPw = (EditText) findViewById(R.id.et_account);
		etPwConfirm = (EditText) findViewById(R.id.et_account_confirm);
		etCode = (EditText) findViewById(R.id.et_code);

		etName.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s.length() == 11) {
					btnGetCode
							.setBackgroundResource(R.drawable.btn_orange_light_selector);
					btnGetCode.setEnabled(true);
				} else {
					btnGetCode
							.setBackgroundResource(R.drawable.btn_gray_light_selector);
					btnGetCode.setEnabled(false);
				}
			}
		});
		//
		btnGetCode = (Button) findViewById(R.id.btn_get_auth_code);
		btnRegister = (Button) findViewById(R.id.btn_register);

		btnGetCode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (StringUtil.isMobileNO(etName.getText().toString().trim())) {
					new UserRequest(mContext, mHandler).checkPhone(etName
							.getText().toString());
				} else {
					ToastHelper.toastShort(mContext, "手机号码格式不正确");
				}
			}
		});

		btnRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (check()) {
					new UserRequest(mContext, mHandler).checkCaptcha(etName
							.getText().toString(), etCode.getText().toString());
				}
				// String title = "注册成功,现在去认证吗?";
				// final DialogAuth dialogAuth = new DialogAuth(mContext,
				// title);
				// dialogAuth.setSelectedListener(new SelectedListener() {
				//
				// @Override
				// public void onConfirm() {
				// dialogAuth.dismiss();
				// startActivity(new Intent(mContext, AuthCarActivity.class));
				// finish();
				// }
				//
				// @Override
				// public void onCancel() {
				// dialogAuth.dismiss();
				// finish();
				// }
				// });
				// dialogAuth.show(getSupportFragmentManager(), "");
			}
		});

		rgType = (RadioGroup) findViewById(R.id.rg_type);
		rgType.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_cargo:
					userType = "1";
					break;
				case R.id.rb_car:
					userType = "0";
					break;

				default:
					break;
				}
			}
		});
	}

	private boolean check() {
		if (StringUtil.isNull(etName.getText().toString())) {
			ToastHelper.toastShort(mContext, "请输入手机号");
			return false;
		}
		if (StringUtil.isNull(etPw.getText().toString())) {
			ToastHelper.toastShort(mContext, "请输入密码");
			return false;
		}

		if (etPw.getText().toString().length() < 6) {
			ToastHelper.toastShort(mContext, "密码长度最少6位");
			return false;
		}

		if (StringUtil.isNull(etPwConfirm.getText().toString())) {
			ToastHelper.toastShort(mContext, "请输入确认密码");
			return false;
		}

		if (!StringUtil.isEqual(etPw.getText().toString(), etPwConfirm
				.getText().toString())) {
			ToastHelper.toastShort(mContext, "密码不一致,请确认");
			return false;
		}

		if (etPw.getText().toString().trim().length() < 6) {
			ToastHelper.toastShort(mContext, "密码长度必须大于六位！");
			return false;
		}

		if (StringUtil.isNull(etCode.getText().toString())) {
			ToastHelper.toastShort(mContext, "请输入验证码");
			return false;
		}
		return true;
	}

	@Override
	protected void onDestroy() {
		if (mTimer != null) {
			mTimer.cancel();
			mTimer = null;
		}
		super.onDestroy();
	}

}
